/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.editor.view.hyperlink;

import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JTabbedPane;
import offset.nodes.client.model.NodeTypes;
import offset.nodes.client.model.PanelContainer;
import offset.nodes.client.model.ServerModel;
import offset.nodes.client.editor.Editor;

/**
 *
 * @author  Walter Lütgenau
 */
public class HyperlinkDialog extends javax.swing.JDialog implements PanelContainer {

    Editor editor;
    public static final int OK = 1;
    public static final int CANCEL = 2;
    public static final int TYPE_INTERNAL = 0;
    public static final int TYPE_EXTERNAL = 1;
    int returnStatus = CANCEL;

    /** 
     * Creates a new HyperlinkDialog
     * 
     * @param editor A reference to the editor
     */
    public HyperlinkDialog(Editor editor) throws Exception {
        super((java.awt.Frame) null, true);
        this.editor = editor;
        initComponents();
        getRootPane().setDefaultButton(okButton);
        init(editor.getMode());
    }

    public int showDialog() {
        setSize(600, 450);
        setVisible(true);
        return returnStatus;
    }

    public void enableOkButton(boolean enable) {
        okButton.setEnabled(enable);
    }

    public int getHyperlinkType() {
        return tabbedPane.getSelectedIndex();
    }

    public InternalHyperlinkInfo getInternalHyperlinkInfo() {
        if (tabbedPane.getSelectedIndex() != TYPE_INTERNAL)
            return null;
        return getInternalHyperlinkPanel().getResult();
    }

    public ExternalHyperlinkInfo getExternalHyperlinkInfo() {
        if (tabbedPane.getSelectedIndex() != TYPE_EXTERNAL)
            return null;
        return getExternalHyperlinkPanel().getResult();
    }

    private void doClose(int retStatus) {
        returnStatus = retStatus;
        setVisible(false);
        dispose();
    }
    
    protected void initInternalHyperlinkPanel() {
        try {
            tabbedPane.setEnabledAt(TYPE_INTERNAL, true);

            String serverUrl = (String) editor.getProperties().get(Editor.PROP_UPLOAD_TO);
            ServerModel serverModel = new ServerModel(new URL(serverUrl));

            getInternalHyperlinkPanel().init(this, serverModel, (NodeTypes) editor.getProperties().get(Editor.PROP_NODE_TYPES),
                    (String) editor.getProperties().get(Editor.PROP_DOCUMENT_PATH));
            getInternalHyperlinkPanel().setText(editor.getBrowserPane().getSelectedText());
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        }
    }

    protected void initExternalHyperlinkPanel() {
        tabbedPane.setEnabledAt(TYPE_EXTERNAL, true);

        getExternalHyperlinkPanel().setText(editor.getBrowserPane().getSelectedText());
        getExternalHyperlinkPanel().init(this);
    }

    public void init(int mode) throws Exception {
        tabbedPane.setEnabledAt(TYPE_INTERNAL, false);
        tabbedPane.setEnabledAt(TYPE_EXTERNAL, false);

        switch (mode) {
            case Editor.MODE_LOCAL_PAGE_EDITOR:
            case Editor.MODE_PAGE_EDITOR:
                initInternalHyperlinkPanel();
                initExternalHyperlinkPanel();
                tabbedPane.setSelectedIndex(TYPE_INTERNAL);
                break;
        }
    }

    public ExternalHyperlinkPanel getExternalHyperlinkPanel() {
        return externalHyperlinkPanel;
    }

    public InternalHyperlinkPanel getInternalHyperlinkPanel() {
        return internalHyperlinkPanel;
    }

    public Editor getEditor() {
        return editor;
    }

    public JTabbedPane getTabbedPane() {
        return tabbedPane;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        tabbedPane = new javax.swing.JTabbedPane();
        internalHyperlinkPanel = new offset.nodes.client.editor.view.hyperlink.InternalHyperlinkPanel();
        externalHyperlinkPanel = new offset.nodes.client.editor.view.hyperlink.ExternalHyperlinkPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setLocationByPlatform(true);

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("offset/nodes/client/editor/view/resources/EditorBundle"); // NOI18N
        okButton.setText(bundle.getString("Button.ok")); // NOI18N
        okButton.setEnabled(false);
        okButton.setMaximumSize(new java.awt.Dimension(67, 23));
        okButton.setMinimumSize(new java.awt.Dimension(67, 23));
        okButton.setPreferredSize(new java.awt.Dimension(67, 23));
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ok(evt);
            }
        });

        cancelButton.setText(bundle.getString("Button.cancel")); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancel(evt);
            }
        });

        tabbedPane.setPreferredSize(new java.awt.Dimension(1000, 488));
        tabbedPane.addTab(bundle.getString("Hyperlink.internal"), internalHyperlinkPanel); // NOI18N
        tabbedPane.addTab(bundle.getString("Hyperlink.external"), externalHyperlinkPanel); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE)))
                .addGap(18, 18, 18))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tabbedPane, javax.swing.GroupLayout.PREFERRED_SIZE, 236, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(19, 19, 19))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void ok(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ok
        doClose(OK);
    }//GEN-LAST:event_ok

    private void cancel(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancel
        doClose(CANCEL);
    }//GEN-LAST:event_cancel
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private offset.nodes.client.editor.view.hyperlink.ExternalHyperlinkPanel externalHyperlinkPanel;
    private offset.nodes.client.editor.view.hyperlink.InternalHyperlinkPanel internalHyperlinkPanel;
    private javax.swing.JButton okButton;
    private javax.swing.JTabbedPane tabbedPane;
    // End of variables declaration//GEN-END:variables
}
